//
//  inviteVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/12/10.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "inviteVC.h"
#import "BPshareSdkView.h"
#import <ShareSDK/ShareSDK.h>
#import "MBProgressHUD+MJ.h"
#import "AccountTool.h"
#import "BPhttpTool.h"
#import "YaoQingTableViewCell.h"
#import "BPLoginViewController.h"


#define shareUrl @"http://ttmy.len.so/home/appinvite/index/u/"

@interface inviteVC ()<UITableViewDataSource, UITableViewDelegate>
{
    MBProgressHUD *_postHud;
}

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSArray *arrAll;

@end

@implementation inviteVC

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [self createData];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseWebView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 10);
    
    self.arrAll = [NSArray array];
    
    [self create];
    
    // Do any additional setup after loading the view.
}


// 创建头部试图
- (void)create{
    
    UIView *HeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 200)];
    [self.view addSubview:HeadView];
    
    // 头部 图片
    UIImageView *imageHead = [[UIImageView alloc] initWithFrame:HeadView.bounds];
    imageHead.image = [UIImage imageNamed:@"yaoqing"];
    [HeadView addSubview:imageHead];
    
    CGFloat backX = 15.0f;
    CGFloat backY = 22.0f;
    CGFloat backW = 30.0f;
    CGFloat backH = 30.0f;
    // 返回按钮
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(backX, backY, backW, backH)];
    [back setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [HeadView addSubview:back];
    
    
//    分享
    UIButton *weixin = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 30, self.view.frame.size.height - 300, 60, 60)];
    [weixin setImage:[UIImage imageNamed:@"Weixin"] forState:UIControlStateNormal];
    [weixin addTarget:self action:@selector(weixin:) forControlEvents:UIControlEventTouchUpInside];
    [HeadView addSubview:weixin];
    
    UIButton *penyouquan = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 30, self.view.frame.size.height - 300, 60, 60)];
    
    [penyouquan addTarget:self action:@selector(pengyou:) forControlEvents:UIControlEventTouchUpInside];
    [penyouquan setImage:[UIImage imageNamed:@"pengyou"] forState:UIControlStateNormal];
    [HeadView addSubview:penyouquan];

    
    // 日期 邀请好友 获得美印币
    // 日期
    UILabel *day = [[UILabel alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 200, 50, 30)];
    day.text = @"日期";
    day.textColor = [UIColor blackColor];
    [self.view addSubview:day];
    
    // 内容
    UILabel *nR = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.view.frame.size.height - 200, 100, 30)];
    nR.text = @"邀请好友";
    nR.textColor = [UIColor blackColor];
    [self.view addSubview:nR];
    
    // 数量
    UILabel *num = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100, self.view.frame.size.height - 200, 100, 30)];
    num.text = @"获得美印币";
    num.textColor = [UIColor blackColor];
    [self.view addSubview:num];
}

- (void)back:(UIButton *)backButton{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)createData{
//    if ([AccountTool account].uid == nil) {
//        [MBProgressHUD showError:@"您还没有登录!"];
//        BPLoginViewController *logo = [[BPLoginViewController alloc] init];
//        logo.block = ^(){
//            NSLog(@"ggg");
//        };
//        [self.navigationController pushViewController:logo animated:YES];
//
//    }else{
    
        NSLog(@"uid = %@", [AccountTool account].uid);
         _postHud = [MBProgressHUD showMessage:@"正在加载数据"];
        [BPhttpTool get:[NSString stringWithFormat:@"%@user/invitelist/uid/%@", BPhttp, [AccountTool account].uid] params:nil success:^(id responseObj) {
            
            if (responseObj == NULL) {
               
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:@"您还没有邀请好友哦!"];
//                [self.navigationController popViewControllerAnimated:YES];
            }
            else{
            
            self.arrAll = responseObj;
            [MBProgressHUD hideHUD];
            [self createTabel];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"加载失败!"];
        }];
//    }

}



- (void)createTabel{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 170, self.view.frame.size.width, self.view.frame.size.height - 170)];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.arrAll.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YaoQingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"yaoqing"];
    if (!cell) {
        cell = [[YaoQingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"yaoqing"];
    }
    cell.Day.text = [[self.arrAll objectAtIndex:indexPath.row] objectForKey:@"create_time"];
    cell.neiRong.text = [[self.arrAll objectAtIndex:indexPath.row] objectForKey:@"mobile"];
    cell.number.text = [[self.arrAll objectAtIndex:indexPath.row] objectForKey:@"status"];
    
    return cell;
}



//- (void)clickShareBtn:(NSString *)btnType
//{
//    
//    if ([btnType isEqualToString:@"sp1"]) {
//        [self shareToApp:nil shareType:ShareTypeWeixiSession];
//    }
//    if ([btnType isEqualToString:@"sp2"]) {
//        [self shareToApp:nil shareType:ShareTypeWeixiTimeline];
//    }
//}

- (void)weixin:(UIButton *)button{
    NSString *imagep = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:@"bpIcon.png"];
    id<ISSContent> publishContent = [ShareSDK content:@"邀请有礼,快领取你的美印币吧"
                                       defaultContent:@""
                                                image:[ShareSDK imageWithPath:imagep]
                                                title:@"天天美印"
                                                  url:[NSString stringWithFormat:@"%@%@",shareUrl,[AccountTool account].uid]
                                          description:@"邀请有礼"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    
    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
   // [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:nil
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:NO
                                                        wxSessionButtonHidden:NO
                                                       wxTimelineButtonHidden:NO
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil                                                     picViewerViewDelegate:nil];
    
    
    //分享类型
    [ShareSDK showShareViewWithType:ShareTypeWeixiSession
                          container:container
                            content:publishContent
                      statusBarTips:YES
                        authOptions:authOptions
                       shareOptions:shareOptions
                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                 
                                 if (state == SSPublishContentStateSuccess)
                                 {
                                     [MBProgressHUD showSuccess:@"发表成功"];
                                 }
                                 else if (state == SSPublishContentStateFail)
                                 {
                                     [MBProgressHUD showError:@"发表失败"];
                                 }
                             }];
}

- (void)pengyou:(UIButton *)pengyquan
{
    NSString *imagep = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:@"bpIcon.png"];
    id<ISSContent> publishContent = [ShareSDK content:@"邀请有礼,快领取你的美印币吧"
                                       defaultContent:@""
                                                image:[ShareSDK imageWithPath:imagep]
                                                title:@"天天美印"
                                                  url:[NSString stringWithFormat:@"%@%@",shareUrl,[AccountTool account].uid]
                                          description:@"邀请有礼"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    
    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
    //[container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:nil
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:NO
                                                        wxSessionButtonHidden:NO
                                                       wxTimelineButtonHidden:NO
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil                                                     picViewerViewDelegate:nil];
    
    
    //分享类型
    [ShareSDK showShareViewWithType:ShareTypeWeixiTimeline
                          container:container
                            content:publishContent
                      statusBarTips:YES
                        authOptions:authOptions
                       shareOptions:shareOptions
                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                 
                                 if (state == SSPublishContentStateSuccess)
                                 {
                                     [MBProgressHUD showSuccess:@"发表成功"];
                                 }
                                 else if (state == SSPublishContentStateFail)
                                 {
                                     [MBProgressHUD showError:@"发表失败"];
                                 }
                             }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
